<?php

class Commerce_CartController extends Commerce_Controller_Action {

	function init(){	
		parent::init();				
	}
	
	/*
	function indexAction() {
		$checkout_session_id = $_COOKIE['frontend'];
		$request = new Cts_Request($this->getRequest());
		$cart = new CommerceMagentoCart($checkout_session_id);
dd($cart);
// dd($cart->getCheckoutSessionId()."\n\n".$cart->getItems());

		$this->view->cart_items = $cart->getItems();
	}

	function addAction() {
		$session_id = $_COOKIE['frontend'];
		if (is_null($session_id) || trim($session_id) == '') {
			// bounce to Magento to create that cookie and then come back ??
		}
		$request = new Cts_Request($this->getRequest());
		if ($request->has('product')) {
			$product = $request->product;
			$qty = '1';
			if ($request->has('qty')) {
				$qty = $request->qty;
			}
			$cart = new CommerceMagentoCart($session_id);
dd($cart->addItem($product, $qty));
			// success
			// redirect
		} else {
			// fail
		}
	}
	*/

	function indexAction(){
		return;
	}
	
	function addAction(){
		$Cart = new Magento_Curl_Cart();
		$Cart->setLocale( $this->locale_code );
		$Cart->debug_mode = true;
		$request = new Cts_Request($this->getRequest());
		
		if( $request->has('product_id') && $request->has('qty') ){
			$add = $Cart->addProduct( $request->product_id, (int)abs(ceil($request->qty)) );
			
			if( $request->has('is_ajax') ){
				echo '1';
				exit;
			} else {
				$this->_redirect( '/commerce/catalog/product/id/'.$request->product_id.'/success/1' );
			}
		} else {
			die('No product ID.');
		}
	}
	
	function removeAction() {
		$Cart = new Magento_Curl_Cart();
		$Cart->setLocale( $this->locale_code );
		
		$request = new Cts_Request($this->getRequest());
		
		if( $request->has('id')){
				$remove = $Cart->removeProduct( $request->id );
			if ($request->has('is_ajax')) {
				$this->view->cart_product_removed = ($remove) ? '1' : '0';
			} else {
				$this->_redirect( $_SERVER["HTTP_REFERER"] );
			}
		} else {
			$this->view->cart_product_removed = '0';
		}		
	}

	function emptyAction() {
		$Cart = new Magento_Curl_Cart();
		$Cart->setLocale( $this->locale_code );
		$Cart->emptyCart();
	}

	function minicartAction() {
		## The data for this template is collected in commerce\lib\Commerce\Plugins\Minicart.php
		return;
	}
}
